package com.mobi.Survey.lib.activities;

import java.io.File;
import java.util.Date;

import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

import com.mobi.Survey.lib.R;
import com.mobi.Survey.lib.db.AlarmTable;
import com.mobi.Survey.lib.model.AlarmPojo;
import com.mobi.Survey.lib.model.AlarmRepeatPojo;
import com.mobi.Survey.lib.utils.Helper;

public class AlarmService extends Service {

	private NotificationManager mManager;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		mManager = (NotificationManager) this.getApplicationContext()
				.getSystemService(Application.NOTIFICATION_SERVICE);

		AlarmPojo attr = null;
		if (null != intent && null != intent.getExtras()) {
			attr = intent.getExtras().getParcelable("attributes");

			if (null == attr) {
				return;
			}
			AlarmTable table = new AlarmTable(getApplicationContext());
			if (!table.isExist(attr.getId())) {
				table.closeDB();
				return;
			}
			if (table.isManuallyClicked(attr.getId())) {
				table.updateManualClick(attr.getId(), false);
			}

			if (attr.isWithRespectToCurrentTime()) {
				table.setWRTCurrentTimeFalseByAlarmId(attr.getId());
			}
			table.closeDB();
			
			if (attr.isRepeat() && attr.getEnd() > System.currentTimeMillis()) {
				long triggerTime = AlarmRepeatPojo.getMilliSecondsUntil(attr.getRepeatType(), attr.getTriggerTime(), 1);

				attr.setPendingIntentId(attr.getPendingIntentId());
				attr.setTriggerTime(triggerTime);
				Log.i("Next Trigger Time", new Date(triggerTime).toLocaleString());
				Helper.setAlarm(getApplication(), attr);
			}

			if(AbstractScreen.notification(this))
			displayNotification("Survey Form", AlarmScreen.class, attr);
			intent.getExtras().clear();

		}

	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private void displayNotification(String contentTitle, Class<?> cls, AlarmPojo attr) {
		Notification notifyDetails = new Notification(R.drawable.ic_action_alarms, "Survey", System.currentTimeMillis());
		File f = null;
		if(null != attr.getAudioUrl()){
			f = new File(Helper.getTempFile(Helper.getHashNameFromUrl(attr.getAudioUrl()) + ""));
		}
		
		if(null != f && f.exists()){
			notifyDetails.sound = Uri. fromFile(f) ;	
		}else{
			notifyDetails.defaults |= Notification.DEFAULT_SOUND;
		}

		notifyDetails.defaults |= Notification.DEFAULT_LIGHTS;

		Intent intent = new Intent(this, cls);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
		
		intent.putExtra("attributes", attr);
		PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), attr.getPendingIntentId(),
				intent, PendingIntent.FLAG_ONE_SHOT);
		notifyDetails.setLatestEventInfo(getApplicationContext(), contentTitle, attr.getMessage(), contentIntent);

		mManager.notify(attr.getPendingIntentId(), notifyDetails);
	}

}
